package user;

import book.Book;
import book.Library;
import book.PairOfUidAndBookId;
import utils.AnalyzingBorrowedBook;
import utils.ScannerSingleton;
import constant.Constant;

import java.io.IOException;
import java.util.Scanner;

import static constant.Constant.BORROW_BOOK_MAX_NUM;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/7/18
 * Time: 02:11
 */
public class Normal extends User {
    public Scanner scanner;
    //存储图书借阅信息
    private PairOfUidAndBookId[] pairOfUidAndBookIds;
    //当前图书的借阅量
    private int borrowCount;
    public Library library = Library.getLibrary();
    private final AnalyzingBorrowedBook analyzingBorrowedBook =  new AnalyzingBorrowedBook();

    public Normal(String name, int ID) {
        super(name, ID, "普通用户");
        scanner = ScannerSingleton.getScanner();
    }

    public void loadAllBorrowBook(){
        PairOfUidAndBookId[] allBorrowedBook;
        try {
            //1.先加载文件当中的借阅信息
            allBorrowedBook = analyzingBorrowedBook.loadObject(Constant.BORROWED_BOOK_FILE_NAME);
            //2.默认已借阅的图书数组⼤⼩为BORROW_BOOK_MAX_NUM，这里也可以定义到常量类
            pairOfUidAndBookIds = new PairOfUidAndBookId[BORROW_BOOK_MAX_NUM];
            //3.没有读取到已借阅的图书信息
            if (allBorrowedBook== null) {
                borrowCount = 0;
            } else {
                //4.查看实际读取到的数组长度是多少？
                int allBorrowedBookLen= allBorrowedBook.length;
                //5.如果读取到了10本书被借阅 但是当前borrowedBooks数组⻓度⼩于10
                if (allBorrowedBookLen> pairOfUidAndBookIds.length) {
                    //6.按照实际情况进⾏分配数组内存
                    pairOfUidAndBookIds = new PairOfUidAndBookId[allBorrowedBookLen];
                }
                //7.把数据拷贝回到已借阅图书信息的数组当中
                System.arraycopy(allBorrowedBook, 0, pairOfUidAndBookIds, 0, allBorrowedBookLen);
                //8.更新当前实际借阅书籍的书籍数量
                borrowCount = allBorrowedBookLen;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private void storeBorrowedBook() {
        try {
            analyzingBorrowedBook.storeObject(pairOfUidAndBookIds, Constant.BORROWED_BOOK_FILE_NAME);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int menu() {
        System.out.println("普通用户 " + this.getName() + " 的操作菜单");
        System.out.println("1.查找图书");
        System.out.println("2.打印所有图书");
        System.out.println("3.退出系统");
        System.out.println("4.借阅图书");
        System.out.println("5.归还图书");
        System.out.println("6.查看个人的借阅情况");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    //借阅图书
    public void borrowBook() {
        scanner.nextLine();
        System.out.println("请输⼊你要借阅图书的id：");
        int bookId = scanner.nextInt();
        scanner.nextLine();
        //如果书架没有书 不能借阅
        if (library.bookCount == 0) {
            System.out.println("书架没有书籍暂且不能借阅");
            return;
        }
        loadAllBorrowBook();
        //1.判断要借阅的图书是否存在于书架
        Book book = library.searchById(bookId);
        if (book == null) {
            System.out.println("这本书不存在,图书Id：" + bookId);
            return;
        }
        //2.开始准备借阅图书
        for (int i = 0; i < borrowCount; i++) {
            PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
            if (pairOfUidAndBookId.getBookId() == book.getBookId()) {
                if (pairOfUidAndBookId.getUserId() == this.getUserID()){
                    System.out.println("这本书已经被你借阅了,图书Id： + bookId");
                    return;
                }else {
                    System.out.println("这本书已经被别人借阅了,图书Id： + bookId");
                    return;
                }
            }
        }
        library.borrowBook(bookId);
        //封装对象写到借阅表当中
        PairOfUidAndBookId pairOfUidAndBookId = new PairOfUidAndBookId(this.getUserID(), book.getBookId());
        pairOfUidAndBookIds[borrowCount] = pairOfUidAndBookId;
        borrowCount++;
        //存储借阅图书
        storeBorrowedBook();
        System.out.println("借阅成功！");
    }
    //归还图书
    public void returnBook() {
        loadAllBorrowBook();
        if (borrowCount == 0) {
            System.out.println("⽬前没有用户借阅过书籍");
            return;
        }
        scanner.nextLine();
        System.out.println("请输⼊你要归还图书的id：");
        int bookId = scanner.nextInt();
        scanner.nextLine();
        //判断要借阅的书是否是已经被自己借阅过了
        Book book = library.searchById(bookId);
        if(book == null) {
            System.out.println("没有该id的相关书籍：" + bookId);
            return;
        }
        for (int i = 0; i < borrowCount; i++) {
            //如果2本书的ID相同,则认为是同⼀本书
            if (pairOfUidAndBookIds[i].getBookId()==book.getBookId()) {
                //如果借阅用户也⼀样
                if (getUserID() == pairOfUidAndBookIds[i].getUserId()) {
                    library.returnBook(bookId);
                    System.out.println("图书：" + book.getTitle() + "已成功归还");
                    //用最后⼀本替换归还的书
                    pairOfUidAndBookIds[i] = pairOfUidAndBookIds[borrowCount - 1];
                    //清空最后⼀个
                    pairOfUidAndBookIds[borrowCount - 1] = null;
                    borrowCount--;
                    storeBorrowedBook();
                }else{
                    //如果借阅用户不⼀样
                    System.out.println("该书籍不是你借阅的书籍,不能归还："+book.getTitle());
                }
                return;
            }
        }
        System.out.println("你没有借阅该书籍,不需要归还,书籍ID为："+bookId);
    }
    //查看个⼈借阅情况
    public void viewBorrowBooks() {
        //读取当前借阅所有用户的借阅书籍的情况
        loadAllBorrowBook();
        System.out.println("您的借阅情况如下：");
        if (borrowCount == 0) {
            System.out.println("目前没有借阅记录.....");
        } else {
            boolean flg = false;
            for (int i = 0; i < borrowCount; i++) {
                //这⾥只能查看属于自己借阅的情况
                //用户ID相同的情况下，使⽤书籍ID查询书籍
                if(pairOfUidAndBookIds[i].getUserId() == this.getUserID()) {
                    flg = true;
                    Book book = library.searchById(pairOfUidAndBookIds[i].getBookId());
                    System.out.println(book);
                }
            }
            if(!flg) {
                System.out.println("你没有借阅过书籍！");
            }
        }
    }
}
